package com.mytech.cslive.commands;

public abstract class Command {
	private static final char START_DELIMITER = '<';
	private static final char COMMAND_NAME_SEPERATOR = ':';
	private static final char END_DELIMITER = '>';
	private static final char PARAMETERS_SEPERATOR = ';';
	
	private String name;
	
	protected static char getStartDelimiter() {
		return START_DELIMITER;
	}

	protected static char getEndDelimiter() {
		return END_DELIMITER;
	}

	protected static char getParametersSeperator() {
		return PARAMETERS_SEPERATOR;
	}

	public static char getCommandNameSeperator() {
		return COMMAND_NAME_SEPERATOR;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Command(String name) {
		this.name = name;
	}
	
	protected String addEndDelimeter(String string) {
		return (string + getEndDelimiter());
	}
	
	protected String addParameters(String commandString, CommandParameter... commandParameters) {
		for (int index = 0; index < commandParameters.length; index++) {
			commandString += commandParameters[index].toString() + getParametersSeperator();
		}
		
		if (commandParameters.length > 0) {
			// Erase last parameter seperator
			commandString = commandString.substring(0, commandString.length() - 1);
		}
		
		return (commandString);
	}
	
	@Override
	public String toString() {
		return (getStartDelimiter() + name + getCommandNameSeperator());
	}
	
	public byte[] toByteArray() {
		return (this.toString().getBytes());
	}
}
